package com.fulihui.integralbusiness.handler;

import com.fulihui.information.api.ContentCommentsReplyService;
import com.fulihui.information.api.ContentCommentsService;
import com.fulihui.information.dto.InfoContentCommentsDTO;
import com.fulihui.information.dto.InfoContentCommentsReplyDTO;
import com.fulihui.information.request.ContentCommentsQueryRequest;
import com.fulihui.information.request.ContentCommentsReplyQueryRequest;
import com.fulihui.integral.core.enums.ISChannelType;
import com.fulihui.integral.core.request.RewardPointsReq;
import com.fulihui.integralbusiness.constants.IntegralBusinessFlags;
import org.near.servicesupport.result.TPageResult;
import org.near.toolkit.common.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Map;

import static org.near.servicesupport.util.ServiceResultUtil.checkResult;

/**
 * Created by LM on 2017/4/21.
 */
@Component("commentsIntegralBusinessHandler")
public class CommentsIntegralBusinessHandler extends AbstractIntegralBusinessHandler {
    private transient final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private ContentCommentsService      contentCommentsService;
    @Autowired
    private ContentCommentsReplyService contentCommentsReplyService;

    @Override
    public void handle(Map<String, Object> msg) {
        String userId = msg.get("userId").toString();
        if (StringUtil.isBlank(userId)) {
            return;
        }
        // 查询用户评论
        ContentCommentsQueryRequest requestComments = new ContentCommentsQueryRequest();
        requestComments.setUserId(userId);
        TPageResult<InfoContentCommentsDTO> commentsResult = contentCommentsService
                .queryPage(requestComments);
        checkResult(commentsResult);
        if (!CollectionUtils.isEmpty(commentsResult.getValues())
            && commentsResult.getValues().size() > 1) {
            return;
        }
        // 查询用户评论回复
        ContentCommentsReplyQueryRequest requestCommentsReply = new ContentCommentsReplyQueryRequest();
        requestCommentsReply.setUserId(userId);
        TPageResult<InfoContentCommentsReplyDTO> commentsReplyResult = contentCommentsReplyService
                .queryPage(requestCommentsReply);
        checkResult(commentsReplyResult);
        if (!CollectionUtils.isEmpty(commentsReplyResult.getValues())
            && commentsReplyResult.getValues().size() > 1) {
            return;
        }
        // 奖励积分
        RewardPointsReq rewardPointsReq = new RewardPointsReq();
        rewardPointsReq.setUserId(userId);
        rewardPointsReq.setChannel(ISChannelType.FIRST_COMMENT.getCode());
        userBehaviorService.rewardPoints(rewardPointsReq);
    }

    @Override
    IntegralBusinessFlags getFlag() {
        return IntegralBusinessFlags.COMMENTS;
    }

}
